﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LotteCinema.Admin
{
    public partial class QuanLyRapPhim1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                LaydsRP();
        }
        public void LaydsRP()
        {
            DataTable ds_rp = Timkiem_MA("");
            
            DDLMaRap.Items.Add("");
            for (int i = 0; i < ds_rp.Rows.Count; i++)
            {
                string str = ds_rp.Rows[i]["Marap"].ToString();
                DDLMaRap.Items.Add(str);
            }
            grRapPhim.DataSource = ds_rp;
            grRapPhim.DataBind();
        }

        protected void DDLMaRap_SelectedIndexChanged(object sender, EventArgs e)
        {
            //string s = "select * from rapphim where marap='" + DDLMaRap.SelectedItem.ToString() + "'";
            DataTable dt = Timkiem_MA(DDLMaRap.SelectedItem.ToString());
            if (dt.Rows.Count != 0)
            {
                txtTenRap.Text = dt.Rows[0][1].ToString();
                txtDiaChi.Text = dt.Rows[0][2].ToString();
            }
        }

        protected void imThem_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@tenrap", txtTenRap.Text.ToString()));
                sqlPara.Add(new SqlParameter("@diachi", txtDiaChi.Text.ToString()));
                CDatastore.ThucHienStore("SP_INSERT_RAPPHIM", sqlPara);
                lblThongBao.Text = "Bạn đã thêm một rạp phim thành công";
                DDLMaRap.Items.Clear();
                LaydsRP();
                
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void imXoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@marap", DDLMaRap.Text.ToString()));
                CDatastore.ThucHienStore("SP_DELETE_RAPPHIM", sqlPara);
                lblThongBao.Text = "Bạn đã xóa Mã Rạp " + DDLMaRap.Text + " thành công";
                DDLMaRap.Items.Clear();
                LaydsRP();
                txtDiaChi.Text = null;
                txtTenRap.Text = null;

            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void grRapPhim_SelectedIndexChanged(object sender, EventArgs e)
        {

            DDLMaRap.Text = grRapPhim.SelectedRow.Cells[1].Text.ToString();
            txtTenRap.Text = grRapPhim.SelectedRow.Cells[2].Text.ToString();
            txtDiaChi.Text = grRapPhim.SelectedRow.Cells[3].Text.ToString();
        }

        protected void imSua_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@marap", DDLMaRap.Text.ToString()));
                sqlPara.Add(new SqlParameter("@tenrap", txtTenRap.Text.ToString()));
                sqlPara.Add(new SqlParameter("@diachi", txtDiaChi.Text.ToString()));
                CDatastore.ThucHienStore("SP_UPDATE_RAPPHIM", sqlPara);
                lblThongBao.Text = "Bạn đã cập nhật Mã Rạp Chiếu " + DDLMaRap.Text + " thành công";
                DDLMaRap.Items.Clear();
                LaydsRP();
                txtDiaChi.Text = null;
                txtTenRap.Text = null;
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

    

        public DataTable Timkiem_Ten(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@tenrap", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_RAPPHIM_TENRAP", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_RAPPHIM_TENRAP");
        }
        public DataTable Timkiem_MA(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@MArap", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_RAPPHIM_MARAP", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_RAPPHIM_MARAP");
        }

        protected void grRapPhim_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grRapPhim.PageIndex = e.NewPageIndex;
            grRapPhim.DataBind();
            LaydsRP();
        }

        protected void btTimKiem_Click(object sender, EventArgs e)
        {
            DataTable dt = Timkiem_Ten(txtTenRap.Text);
            grRapPhim.DataSource = dt;
            grRapPhim.DataBind();
        }
    }
}